//设置

/**
 * 模型视图
 */
interface ICredentialViewModel extends IBaseViewModel<ICredentialQueryTerms>{
    /**
     * ui-grid控制
     */
    gridOptions: uiGrid.IGridOptionsOf<credentialListItemResponse>;
    /**
     * AppKey
     */
    appKey: string;
    /**
     * AppSecrect
     */
    appSecrect: string;
    /**
     * 添加
     */
    addDate: () =>void;
    /**
     * 删除
     */
    deleteDate: (id: string) => void;
    /**
     * 刷新列表
     */
    refresh: () => void;
    /**
     * 生成AppKey,AppSecrect
     */
    generateRandomCredential: () => void;
}

/**
 * 查询条件
 */
interface ICredentialQueryTerms{
}

class credentialController extends ListController<ICredentialViewModel,ICredentialQueryTerms>{
    gridApi: uiGrid.IGridApiOf<credentialListItemResponse>;

    constructor($scope: ICredentialViewModel,private credentialService: ICredentialService,$ngBootbox: BootboxService)
    {
        super($scope);
    }

    init(): void{
        super.init();
        this.$scope.appKey = '';
        this.$scope.appSecrect = '';
        this.$scope.addDate = this.showDialogAddDate;
        this.$scope.deleteDate = this.showDialogDeleteDate;
        this.$scope.refresh = this.refresh;
        this.$scope.generateRandomCredential =this.generateRandomCredential;

        this.$scope.gridOptions = {
            enableSorting: false,
            useExternalPagination: true,
            useExternalSorting: true,
            enableColumnResizing: true,
            paginationPageSizes: [10,20,30],
            columnDefs: [
                {name: "序号", field: "index", enableColumnMenu: false},
                {name: "AppKey", field: "appKey", enableColumnMenu: false},
                {name: "AppSecrect", field: "appSecrect", enableColumnMenu: false},
                {
                    name: "操作",
                    cellFilter: "checkmark",
                    cellTemplate: '<a class="primary fa fa-trash-o text-danger" title="删除" ng-click="grid.appScope.deleteDate(row.entity.id)"></a>',
                    enableColumnMenu: false
                }
            ],
            onRegisterApi: (gridApi) => {
                this.gridApi = gridApi;
                gridApi.pagination.on.paginationChanged(this.$scope, this.paginationChanged);
            }
        };
        this.query();
    }

    loadData(queryTerms: ICredentialQueryTerms, skip:number, count:number): void {
        this.credentialService.getList(skip,count).then(result => {
            this.$scope.gridOptions.totalItems = result.data.total;
            this.$scope.gridOptions.data = result.data.list;
            
            if(skip == 0){
                this.$scope.gridOptions.paginationCurrentPage = 1;
            }
            this.$scope.querying = false;
        }, error => {
            this.$scope.querying = false;
        });
    }

    /**
     * 生成AppKey,AppSecrect
     */
    generateRandomCredential():void{
        this.$scope.appKey = this.makeId(10);
        this.$scope.appSecrect = this.makeId(15);
    }
    /**
     * 添加
     */
    showDialogAddDate(): void{
        $ngBootbox.confirm("确定保存该数据吗？").then(e => {
            this.credentialService.add(
                this.$scope.appKey,
                this.$scope.appSecrect
            ).then(result => {
                this.query();
                $('#myModal').modal('hide');
            })
        });
    }
    
    /**
     * 删除
     */
    showDialogDeleteDate(id:string): void{
        $ngBootbox.confirm("是否删除该权限？").then(e => {
            this.credentialService.delete(id).then(result => {
                this.query();
            });
        });
    }
    /**
     * 刷新
     */
    refresh():void{
        this.query();
    }

    /**
     *生成id数据 
     */    
    makeId(count:number):string{
        var text = "";
        var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        for (var i = 0; i < count; i++) {
            text += possible.charAt(Math.floor(Math.random() * possible.length));
        }
        return text;
    }

}

app.controller('credentialController',['$scope','credentialService','$ngBootbox',credentialController]);